package com.app.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.app.game.control.HostGameController;
import com.app.game.control.NetworkHostGameController;
import com.app.task.Task;
import java.net.Inet4Address;

public class Server extends Task {

	private ServerSocket serverSocket;
	private HostGameController game = new NetworkHostGameController();
	private int id = 0;
	private boolean local ;

	public Server(int port,boolean local) {
		this.local = local;
		try {
			serverSocket = new ServerSocket(port);
                        
		} catch (IOException e) {

			e.printStackTrace();
		}
		game = new NetworkHostGameController(local);
	}

	@Override
	protected void task() {
		try {
                        System.out.println("Server is waiting");
			Socket socket = serverSocket.accept();
                        System.out.println(socket.getInetAddress().getHostAddress());
			ClientHandler handler = new ClientHandler(socket, id);
			Thread.sleep(2000);
			game.addClient(handler);
			id++;

			//Thread.sleep(2000);

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	public HostGameController getGame() {
		return game;
	}

	public void setGame(HostGameController game) {
		this.game = game;
	}

}
